package com.ruoyi.swla2.controller;

import java.util.Comparator;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.swla2.domain.Zone;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.swla2.domain.GisLayer;
import com.ruoyi.swla2.service.IGisLayerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * GIS管网-图层Controller
 *
 * @author gdqt
 * @date 2023-11-26
 */
@RestController
@RequestMapping("/swla2/gisLayer")
public class GisLayerController extends BaseController {
    @Autowired
    private IGisLayerService gisLayerService;

    /**
     * 查询区域管理列表
     */
    @PreAuthorize("@ss.hasAnyPermi('pipe:manage,pipe:analysis')")
    @GetMapping("/listTree")
    public AjaxResult listTree(GisLayer gisLayer) {
        List<GisLayer> list = gisLayerService.selectGisLayerList(gisLayer);
        list.sort(Comparator.comparing(GisLayer::getLayerType, Comparator.reverseOrder()).thenComparing(GisLayer::getSort));
        return success(transformToTreeList(list));
    }


    /**
     * 查询GIS管网-图层列表
     */
    @PreAuthorize("@ss.hasAnyPermi('pipe:manage,pipe:analysis')")
    @GetMapping("/list")
    public TableDataInfo list(GisLayer gisLayer) {
        List<GisLayer> list = gisLayerService.selectGisLayerList(gisLayer);
        return getDataTable(list);
    }

    /**
     * 导出GIS管网-图层列表
     */
    @PreAuthorize("@ss.hasAnyPermi('pipe:manage,pipe:analysis')")
    @Log(title = "GIS管网-图层", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GisLayer gisLayer) {
        List<GisLayer> list = gisLayerService.selectGisLayerList(gisLayer);
        ExcelUtil<GisLayer> util = new ExcelUtil<GisLayer>(GisLayer.class);
        util.exportExcel(response, list, "GIS管网-图层数据");
    }

    /**
     * 获取GIS管网-图层详细信息
     */
    @PreAuthorize("@ss.hasAnyPermi('pipe:manage,pipe:analysis')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(gisLayerService.selectGisLayerById(id));
    }

    /**
     * 新增GIS管网-图层
     */
    @PreAuthorize("@ss.hasAnyPermi('pipe:manage,pipe:analysis')")
    @Log(title = "GIS管网-图层", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GisLayer gisLayer) {
        return toAjax(gisLayerService.insertGisLayer(gisLayer));
    }

    /**
     * 修改GIS管网-图层
     */
    @PreAuthorize("@ss.hasAnyPermi('pipe:manage,pipe:analysis')")
    @Log(title = "GIS管网-图层", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GisLayer gisLayer) {
        return toAjax(gisLayerService.updateGisLayer(gisLayer));
    }

    /**
     * 删除GIS管网-图层
     */
    @PreAuthorize("@ss.hasAnyPermi('pipe:manage,pipe:analysis')")
    @Log(title = "GIS管网-图层", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(gisLayerService.deleteGisLayerByIds(ids));
    }


    private List<Tree<String>> transformToTreeList(List<GisLayer> nodeList) {
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setParentIdKey("pid");
        treeNodeConfig.setChildrenKey("children");
        treeNodeConfig.setWeightKey("sort");

        List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig, (treeNode, tree) -> {
            tree.setId(treeNode.getId().toString());
            tree.setParentId(treeNode.getPid().toString());
            tree.setName(treeNode.getLayerName());
            tree.putExtra("layerType", treeNode.getLayerType());
            tree.putExtra("layerIcon", treeNode.getLayerIcon());
            tree.putExtra("status", treeNode.getStatus());
            tree.putExtra("sort", treeNode.getSort());
            tree.putExtra("disabled", treeNode.getStatus() == 0);
        });

        return treeNodes;
    }
}
